program Sum;

{$APPTYPE CONSOLE}

uses
  SysUtils;
type
  pelement = ^element;
  element = record
    k: integer;
    a: integer;
    next: pelement;
  end;


procedure AddToBeg (var head:pelement; a, k:integer);
var tmp: pelement;
begin
  tmp:=head;
  new (head);
  head^.next:=tmp;
  head^.a:=a;
  head^.k:=k;
end;

procedure AddToEnd (var head: pelement; a, k: integer);
var cur: pelement;
begin
  if (head <> nil) then
  begin
    cur:=head;
    while (cur^.next <> nil) do
      cur:=cur^.next;
    new (cur^.next);
    cur^.next^.a:=a;
    cur^.next^.k:=k;
    cur^.next^.next:=nil;
    end else
      AddToBeg (head, a, k);
end;

function Sum2 (cur1, cur2: pelement):pelement;
var head: pelement;
     flag: boolean;
begin
  head:=nil;
  while (cur1<>nil) and (cur2<>nil) do
  begin
      flag:=false;
      if (cur1^.k > cur2^.k) then
      begin
      AddToEnd (head, cur1^.a, cur1^.k);
      cur1:=cur1^.next;
      flag:=true;
      end;
      if (flag=false) then
      if (cur1^.k < cur2^.k) then
      begin
      AddToEnd (head, cur2^.a, cur1^.k);
      cur2:=cur2^.next;
      flag:=true;
      end;
      if (flag=false) then
      if (cur1^.k = cur2^.k) then
      begin
      AddToEnd (head, cur1^.a + cur2^.a, cur1^.k);
      cur1:=cur1^.next;
      cur2:=cur2^.next;
      end;
  end;
  while (cur1 <> nil) do
  begin
    AddToEnd (head, cur1^.a, cur1^.k);
    cur1:=cur1^.next;
  end;
  while (cur2 <> nil) do
  begin
    AddToEnd (head, cur2^.a, cur2^.k);
    cur2:=cur2^.next;
  end;
  sum2:=head;
end;

procedure PrintList (head: pelement);
var cur: pelement;
begin
  cur:=head;
  while (cur<>nil) do
  begin
    Writeln (cur^.a, ' ', cur^.k);
    cur:=cur^.next;
  end;
end;

var
  cur1, cur2, t, t1, t2: pelement;
  fin: textfile;
  n, a, k, m, i: integer;
begin
  cur1:=nil;
  cur2:=nil;
  t2:=nil;
  t1:=nil;
  Assignfile (fin, 'input.txt');
  Reset (fin);
  readln (fin, n);
  for i:= 1 to n do
    begin
      readln (fin, a, k);
      AddToEnd (cur1, a, k);
    end;
  readln (fin, m);
  for i:= 1 to m do
    begin
      readln (fin, a, k);
      AddToEnd (cur2, a, k);
    end;
  t:=cur2;
  while (t<>nil) do
  begin
     AddToBeg (t1, cur1^.a * t^.a, cur1^.k + t^.k);
     t:=t^.next;
  end;
  cur1:=cur1^.next;
  while (cur1<>nil) do
  begin
    t:=cur2;
    while (t<>nil) do
    begin
       AddToEnd (t2, cur1^.a * t^.a, cur1^.k + t^.k);
       t:=t^.next;
    end;
    t1:= sum2(t1, t2);
    cur1:=cur1^.next;
  end;

  PrintList (t1);
  Closefile (fin);
  readln;
end.
